#include<iostream>
#include<ostream>
using std::cout;
using std::endl;
using std::ostream;



class Point{

public:
    static Point* Instance(int x,int y){
        if(_point == nullptr){
            _point = new Point(x,y);
        }
        return _point;
    }
    friend
    ostream& operator<<(ostream &ofs,const Point &rhs);
    friend
    class AutoRelease;
private:
    Point(int x,int y)
    :_x(x)
    ,_y(y)
    {}
    ~Point(){
        cout<<"~point"<<endl;
        if(_point){
            delete _point;
            _point = nullptr;
        }
    }
    Point(const Point&rhs) = delete;
    Point& operator= (const Point&rhs) = delete;
private:
    int _x;
    int _y;
    static Point* _point;
};
Point* Point::_point = nullptr;
//<<
ostream& operator<<(ostream &ofs,const Point &rhs){
    ofs<<"Point :"<<rhs._x<<" "<<rhs._y;
    return ofs;
}
class AutoRelease{

public:
    AutoRelease(Point* p)
    :_p(p)
    {
        cout<<"AutoRelease (Point* p)"<<endl;
    }
    //友元就好了
    ~AutoRelease(){
        if(_p){
            delete _p;
            _p = nullptr;
        }
    }
private:
    Point* _p;
};



int main(void){

    Point *p = Point::Instance(3,4);  
    cout<<*p<<endl;
    AutoRelease Autto(p);
}